package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApUnlikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApUnlikesBehaviorService;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper, ApUnlikesBehavior>
        implements ApUnlikesBehaviorService {

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    /**
     * 存储不喜欢的数据
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLike(UnLikesBehaviorDto dto) {
        //参数校验
        if (dto == null || dto.getArticleId() == null ||
                dto.getType() < 0 || dto.getType() > 1
        ) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //查询实体
        ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), null);
        if (behaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        ApUnlikesBehavior unlikesBehavior = getOne(Wrappers.<ApUnlikesBehavior>lambdaQuery()
                .eq(ApUnlikesBehavior::getEntryId, behaviorEntry.getId())
                .eq(ApUnlikesBehavior::getArticleId, dto.getArticleId())
        );

        if (unlikesBehavior == null && dto.getType().equals(ApUnlikesBehavior.Type.CANCEL.getCode())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "暂无不喜欢");
        }

        if (unlikesBehavior != null && dto.getType().equals(ApUnlikesBehavior.Type.UNLIKE.getCode())
                && ApUnlikesBehavior.Type.UNLIKE.getCode() == dto.getType().shortValue()
                && dto.getType().intValue() == unlikesBehavior.getType().intValue()
        ) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "重复不喜欢");
        }

        //保存不喜欢
        if (unlikesBehavior == null && dto.getType().equals(ApUnlikesBehavior.Type.UNLIKE.getCode())){
            unlikesBehavior = new ApUnlikesBehavior();
            unlikesBehavior.setArticleId(dto.getArticleId());
            unlikesBehavior.setCreatedTime(new Date());
            unlikesBehavior.setEntryId(behaviorEntry.getId());
            unlikesBehavior.setType(dto.getType());

            save(unlikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {//取消保存
            unlikesBehavior.setType(dto.getType());
            updateById(unlikesBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
    }
}
